package com.hema.auction.activity;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.hema.auction.R;
import com.hema.auction.base.BasePhotoActivity;
import com.hema.auction.http.HttpTag;
import com.hema.auction.http.biz.HttpManager;
import com.hema.auction.utils.GlideUtils;
import com.hema.auction.utils.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

/**
 * 描述:我的信息
 * 作者:john
 */
public class MyInfoActivity extends BasePhotoActivity {

    @BindView(R.id.iv_avatar)
    CircleImageView ivAvatar;
    @BindView(R.id.tv_nick_name)
    TextView tvNickName;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    private String newAvatar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_info);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        GlideUtils.loadAvatar(this,profileInfo.getFacePic(),ivAvatar);
        tvPhone.setText(profileInfo.getPhone());
    }

    @Override
    protected void onResume() {
        super.onResume();
        tvNickName.setText(getProfileInfo().getName());
    }

    @OnClick({R.id.ll_avatar, R.id.ll_nick_name, R.id.ll_phone})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_avatar:
                setPhotoNeedCrop(true);
                showSelectDialog();
                break;
            case R.id.ll_nick_name:
                intentTo(EditNickNameActivity.class);
                break;
            case R.id.ll_phone:
                break;
        }
    }

    @Override
    protected void selectPhotoResult(Uri uri) {
        super.selectPhotoResult(uri);
        File file = new File(uri.getPath());
        if (file.exists()) {
            try {
                String data = Utils.encodeBase64File(file);
                isAutoDismissDialog = false;
                showProgressDialog();
                HttpManager.getInstance(this).fileUpload(data, true, this);
            } catch (Exception e) {
                e.printStackTrace();
                showToastSafe(R.string.abnormal_operation);
            }
        } else {
            showToastSafe(R.string.abnormal_operation);
        }
    }

    @Override
    protected void onSuccess(HttpTag httpTag, Call call, JSONObject result) {
        super.onSuccess(httpTag, call, result);
        switch (httpTag) {
            case TAG_FILE_UPLOAD:
                try {
                    if (result.getInt("errcode") == 0) {
                        JSONArray jsonArray = result.getJSONArray("data");
                        if (jsonArray.length() > 0) {
                            newAvatar = jsonArray.getString(0);
                            HttpManager.getInstance(this).updateUserInfo("face_pic", newAvatar, this);
                        }
                    }
                    showToastSafe(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                break;
            case TAG_UPDATE_USER_INFO:
                try {
                    if (result.getInt("errcode") == 0) {
                        profileInfo.setFacePic(newAvatar);
                        updateProfile();
                        GlideUtils.loadAvatar(this,profileInfo.getFacePic(),ivAvatar);
                    }
                    showToastSafe(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                dismissProgressDialog();
                break;
        }
    }
}
